package com.msa.msaportal.dao.impl;
import java.util.List;

import org.hibernate.SQLQuery;







import com.msa.msaportal.common.hibernate.Finder;
import com.msa.msaportal.common.hibernate.HibernateBaseDao;
import com.msa.msaportal.common.page.Pagination;
import com.msa.msaportal.dao.IPortalExampleDao;
import com.msa.msaportal.entity.PortalCssEntity;
import com.msa.msaportal.entity.PortalExample;
import com.msa.msaportal.entity.Portalfooter;
import com.msa.msaportal.entity.PortalheaderEntity;
import com.msa.msaportal.entity.PortletViewRes;
import com.msa.msaportal.entity.Template;

public class PortalExampleDaoImpl extends HibernateBaseDao<PortalExample, Integer> implements IPortalExampleDao {

	@Override
	public PortalExample savePortal(PortalExample portalExample) {

		this.getSession().save(portalExample);
		return portalExample;
	}

	@SuppressWarnings("unchecked")
	public List<Template> getTemplateListByOrgid(String orgId) {

		Finder finder = Finder.create();
		String hql = "from Template bean where bean.orgid=:orgCode";

		finder.append(hql);
		finder.setParam("orgCode", orgId);
		finder.append(" or bean.ispublic=:ispublic");
		finder.setParam("ispublic", true);
		return find(finder);
	}

	@Override
    	protected Class getEntityClass() {
		// TODO Auto-generated method stub
		return null;
	}
//这个
	@SuppressWarnings("unchecked")
	@Override
	public Pagination findPortal(PortalExample portalExample,Integer pageNum, Integer pageCount) {
			
		
		Finder finder = Finder.create();
		String hql = "from PortalExample  bean where 1=1";
		
		 finder.append(hql);
		 finder.append(" and bean.status =:status");
		finder.setParam("status",true);
		if(portalExample.getPortal_name()!=null && !"".equals(portalExample.getPortal_name())){
			finder.append(" and bean.portal_name like:portal_name");
			finder.setParam("portal_name","%"+portalExample.getPortal_name().trim()+"%");
		}
		
		if(portalExample.getCreatetime()!=null && !"".equals(portalExample.getCreatetime())){
			finder.append(" and bean.createtime =:createtime"); 
			finder.setParam("createtime",portalExample.getCreatetime().trim());
		}
		
		if(portalExample.getOrgid()!=null && !"".equals(portalExample.getOrgid())){
			finder.append(" and  bean.orgid=:orgCode"); 
			finder.setParam("orgCode",portalExample.getOrgid().trim());
			
		}
		finder.append(" order by createtime desc,portal_id desc"); 
		
		
		return find(finder,pageNum,pageCount);		
		
	}

	@Override
	public void deleteByPortalId(String portal_id) {
		String hql="from PortalExample where portal_id=?";
		Integer id = Integer.valueOf(portal_id);
		PortalExample portalExample=(PortalExample)super.findUnique(hql, id);
		if (portal_id!=null) {
		this.sessionFactory.getCurrentSession().delete(portalExample);//
		}
		
	}

	@Override
	public void updatePortal(PortalExample portalExample) {
		this.getSession().update(portalExample);
	}

	@Override
	public PortalExample findByPortalid(String portal_id) {

		String hql=" from PortalExample where portal_id=?";
		Integer id = Integer.valueOf(portal_id);
		PortalExample portalExample=(PortalExample)super.findUnique(hql, id);		
		return portalExample;
	}


	@Override
	public List<PortalExample> getAll(Integer pageNum, Integer pageCount) {
		//findByCriteria我这个方法可以用  .getHibernateTemplate().但是要继承这个
		String sql = "select * from Se_Portal";
		SQLQuery sq =  this.getSession().createSQLQuery(sql).addEntity(PortalExample.class);
		sq.setFirstResult((pageNum-1)*pageCount).setMaxResults(pageCount);
		List<PortalExample> list = sq.list();
		
		return list;
	
	}
	
	public void update(PortalExample portalExample) {
		this.getSession().update(portalExample);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PortalExample> getPortalListByorgCode(String orgCode) {
		Finder finder = Finder.create();
		String hql = "from PortalExample bean where bean.orgid=:orgcode";
		finder.append(hql);
		finder.setParam("orgcode", orgCode);
		finder.append(" and bean.status=:status");
		finder.setParam("status", true);
		return find(finder);
	}


@SuppressWarnings("unchecked")
@Override
public List<PortalExample> getPortalByOrgId(String json) {
	
	Finder finder = Finder.create();
	String hql = "from PortalExample bean where bean.orgid=:orgCode";
	finder.append(hql);
	finder.setParam("orgCode", json);
	finder.append(" and bean.status=:status");
	finder.setParam("status", true);
	
	return find(finder);
	
}

@SuppressWarnings("unchecked")
@Override
public List<PortalExample> getPortalAll() {
	
	Finder finder = Finder.create();
	String hql = "from PortalExample bean";
	finder.append(hql);
	
	return find(finder);
	
}

@SuppressWarnings("unchecked")
@Override
public List<PortletViewRes> getportletResListByorgCode(String orgCode) {
	Finder finder = Finder.create();
	String hql = "from PortletViewRes bean where bean.orgid=:orgcode";
	finder.setParam("orgcode", orgCode);
	finder.append(hql);
	finder.append(" or bean.ispublic=:ispublic");
	finder.setParam("ispublic", true);
	return find(finder);
}

@SuppressWarnings("unchecked")
@Override
public List<PortalExample>getPortalNumber() {
	
	Finder finder = Finder.create();
	String hql = "from PortalExample bean where bean.use_on=:orgCode";
	finder.append(hql);
	finder.setParam("orgCode", true);
	return find(finder);
}


@SuppressWarnings("unchecked")
@Override
public List<PortalExample> getPortal() {

		
		Finder finder = Finder.create();
		String hql = "from PortalExample bean where 1=1 ";
		finder.append(hql);
		finder.append(" and bean.status=:status");
		finder.setParam("status", true);
		finder.append(" and bean.use_on=:use_on");
		finder.setParam("use_on", true);		
	    return find(finder);
}

@Override
public PortalCssEntity showPortalCss(Integer cssId) {
	
	String hql="from PortalCssEntity where cssId=?";
	Integer id = Integer.valueOf(cssId);
	PortalCssEntity portalCssEntity=(PortalCssEntity)super.findUnique(hql, id);	
	return portalCssEntity;
}


@Override
public Template showPortalTemp(Integer template_id) {
	String hql="from Template where template_id=?";
	Integer id = Integer.valueOf(template_id);
	Template template =(Template)super.findUnique(hql, id);	
	return template;
}

@Override
public Portalfooter getFooterContent(Integer footerResId) {
	String hql="from Portalfooter where footerres_id=?";
	Integer id = Integer.valueOf(footerResId);
	Portalfooter portalfooter =(Portalfooter)super.findUnique(hql, id);	
	return portalfooter;
}

@Override
public PortalheaderEntity getPortalheaderEntity(Integer headerresid) {
	String hql="from PortalheaderEntity where headerresid=?";
	Integer id = Integer.valueOf(headerresid);
	PortalheaderEntity portalheaderEntity =(PortalheaderEntity)super.findUnique(hql, id);	
	return portalheaderEntity;
}



}
